<?php
	class vote_management extends Zcontroller{
		
		function __construct() {
	        parent::__construct();
	    }
	    
	    /* --- VOTE ITEM CATEGORY --- */
	    
	    /*- SELECT -*/
	    function itemCategory() {
	    	$this->load->model("model_vote_management");
	    	$data['categories']=$this->model_vote_management->selectCategory();
	       	$this->load->template("templates/general","zuka_votes/vote_item_category", "Zuka",$data);
	    }
	    /*- Delete -*/
		function deleteCategory($cat_id) {
	    	$this->load->model("model_vote_management");
	    	$this->model_vote_management->deleteCategory($cat_id);
	       	redirect('vote_management/itemCategory');
	    }
	    /*- SHOW ADD NEW FORM -*/
		function addNewCategoryForm() {
	       	$this->load->template("templates/general","zuka_votes/vote_item_category_add", "Zuka");
	    }
		/*- ADD -*/
		function addCategory() {
			$this->load->model("model_vote_management");
			$cat_desc=$_POST['txtcat'];
			if($cat_desc!=""){
				$result=$this->model_vote_management->existingCategory($cat_desc);
				$data['error']=($result>=1)?"This category already exists":"";
				if ($data['error']!=""){
					$data['cat_desc']=$cat_desc;
					$this->load->template("templates/general","zuka_votes/vote_item_category_add", "Zuka",$data);
				}else{
					$this->model_vote_management->insertCategory($cat_desc);
		       		redirect(site_url("vote_management/itemCategory"));
				}
			}else{
				redirect(site_url("vote_management/addNewCategoryForm"));
			}

	    }
		/*- SHOW UPDATE FORM -*/
		function updateCategoryForm($id) {
	    	$this->load->model("model_vote_management");
	    	$data['categories']=$this->model_vote_management->selectCategory($id);
	       	$this->load->template("templates/general","zuka_votes/vote_item_category_update", "Zuka",$data);
	    }
		function updateCategory() {
			$this->load->model("model_vote_management");
			$cat_id=$_POST['txtcatid'];
			$cat_desc=$_POST['txtcat'];
			if($cat_desc!=""){
				$result=$this->model_vote_management->existingCategory($cat_desc,$cat_id);
				$data['error']=($result>=1)?"This category already exists":"";
				if ($data['error']!=""){
					$data['categories']=$this->model_vote_management->selectCategory($cat_id);
					$data['cat_id']=$cat_id;
					$data['cat_desc']=$cat_desc;
					$this->load->template("templates/general","zuka_votes/vote_item_category_update", "Zuka",$data);
				}else{
		    		$this->model_vote_management->updateCategory($cat_id,$cat_desc);
		       		redirect(site_url("vote_management/itemCategory"));
				}
			}else{
				redirect(site_url("vote_management/updateCategoryForm/".$cat_id));
			}
	    }
	    
	    
	 	/* --- VOTE ITEM --- */
	    
	    /*- SELECT -*/
	    function voteItem() {
	    	$this->load->model("model_vote_management");
	    	$data['items']=$this->model_vote_management->selectItem();
	       	$this->load->template("templates/general","zuka_votes/vote_item", "Zuka",$data);
	    }
	    /*- Delete -*/
		function deleteItem($item_id) {
	    	$this->load->model("model_vote_management");
	    	$this->model_vote_management->deleteItem($item_id);
	       	redirect('vote_management/voteItem');
	    }
	    /*- SHOW ADD NEW FORM -*/
		function addNewItemForm() {
			$this->load->model("model_vote_management");
			$data['categories']=$this->model_vote_management->selectCategory();
	       	$this->load->template("templates/general","zuka_votes/vote_item_add", "Zuka",$data);
	    }
		/*- ADD -*/
		function addItem() {
			$this->load->model("model_vote_management");
			$item_desc=$_POST['txtitem'];
			$cat_id=$_POST['txtcat'];
			if($item_desc!=""){
				$result=$this->model_vote_management->existingItem($item_desc,$cat_id);
				$data['error']=($result>=1)?"This item already exists":"";
				if ($data['error']!=""){
					$data['categories']=$this->model_vote_management->selectCategory();
					$data['item_desc']=$item_desc;
					$this->load->template("templates/general","zuka_votes/vote_item_add", "Zuka",$data);
				}else{
					$this->model_vote_management->insertItem($item_desc,$cat_id);
		       		redirect(site_url("vote_management/voteItem"));
				}
			}else{
				redirect(site_url("vote_management/addNewItemForm"));
			}

	    }
		/*- SHOW UPDATE FORM -*/
		function updateItemForm($id) {
	    	$this->load->model("model_vote_management");
	    	$data['items']=$this->model_vote_management->selectItem($id);
	    	$data['categories']=$this->model_vote_management->selectCategory();
	       	$this->load->template("templates/general","zuka_votes/vote_item_update", "Zuka",$data);
	    }
		function updateItem() {
			$this->load->model("model_vote_management");
			$item_id=$_POST['txtitemid'];
			$item_desc=$_POST['txtitem'];
			$cat_id=$_POST['txtcat'];
			if($item_desc!=""){
				$result=$this->model_vote_management->existingItem($item_desc,$cat_id,$item_id);
				$data['error']=($result>=1)?"This item already exists":"";
				if ($data['error']!=""){
					$data['categories']=$this->model_vote_management->selectCategory();
					$data['items']=$this->model_vote_management->selectItem($item_id);
					$data['item_desc']=$item_desc;
					$data['item_id']=$item_id;
					$data['cat_id']=$cat_id;
					$this->load->template("templates/general","zuka_votes/vote_item_update", "Zuka",$data);
				}else{
					$this->model_vote_management->updateItem($item_id,$item_desc,$cat_id);
		       		redirect(site_url("vote_management/voteItem"));
				}
			}else{
				redirect(site_url("vote_management/updateItemForm/".$item_id));
			}
	    }
	    
	    
	     
	    /* --- VOTE TOPIC --- */
	    
	    /*- SELECT -*/
	    function voteTopic() {
	    	$this->load->model("model_vote_management");
	    	$data['topics']=$this->model_vote_management->selectTopic();
	       	$this->load->template("templates/general","zuka_votes/vote_topic", "Zuka",$data);
	    }
	    /*- DELETE -*/
		function deleteTopic($top_id) {
	    	$this->load->model("model_vote_management");
	    	$this->model_vote_management->deleteTopic($top_id);
	       	redirect('vote_management/voteTopic');
	    }
	    /*- SHOW ADD NEW FORM -*/
		function addNewTopicForm() {
			$this->load->model("model_vote_management");
			$allCategories=$this->model_vote_management->selectCategory();
	    	$data['categories']=$allCategories;
			$categories=$allCategories->result();
	    	$cat_id=(isset($_POST['selcat']))?$_POST['selcat']:$categories[0]->vote_item_cat_id;
	    	$data['cat_id']=$cat_id;
	       	$this->load->template("templates/general","zuka_votes/vote_topic_add", "Zuka",$data);
	    }
		/*- ADD -*/
		function addTopic() {
			$this->load->model("model_vote_management");
			$top_name=$_POST['txttopname'];
			$cat_id=$_POST['selcat'];
			$top_desc=$_POST['txttopdesc'];
			$max_item=$_POST['txtmax'];
			if($top_name!=""){
				$result=$this->model_vote_management->existingTopic($top_name);
				$data['error']=($result>=1)?"This topic already exists":"";
				if ($data['error']!=""){
					$data['top_name']=$top_name;
					$data['top_desc']=$top_desc;
					$data['max_item']=$max_item;
					$this->load->template("templates/general","zuka_votes/vote_topic_add", "Zuka",$data);
				}else{
					$this->model_vote_management->insertTopic($top_name,$cat_id,$top_desc,$max_item);
		       		redirect(site_url("vote_management/voteTopic"));
				}
			}else{
				redirect(site_url("vote_management/addNewTopicForm"));
			}

	    }
		/*- SHOW UPDATE FORM -*/
		function updateTopicForm($id) {
	    	$this->load->model("model_vote_management");
	    	$data['topics']=$this->model_vote_management->selectTopic($id);
			$allCategories=$this->model_vote_management->selectCategory();
	    	$data['categories']=$allCategories;
	    	$categories=$allCategories->result();
	    	$cat_id=(isset($_POST['selcat']))?$_POST['selcat']:null;
	    	$data['cat_id']=$cat_id;
	       	$this->load->template("templates/general","zuka_votes/vote_topic_update", "Zuka",$data);
	    }
		function updateTopic() {
			$this->load->model("model_vote_management");
			$top_id=$_POST['txttopid'];
			$cat_id=$_POST['selcat'];
			$top_name=$_POST['txttopname'];
			$top_desc=$_POST['txttopdesc'];
			$max_item=$_POST['txtmax'];
			if($top_name!=""){
				$result=$this->model_vote_management->existingTopic($top_name,$top_id);
				$data['error']=($result>=1)?"This topic already exists":"";
				if ($data['error']!=""){
					$data['topics']=$this->model_vote_management->selectTopic($top_id);
					$data['top_id']=$top_id;
					$data['top_name']=$top_name;
					$data['top_desc']=$top_desc;
					$data['max_item']=$max_item;
					$this->load->template("templates/general","zuka_votes/vote_topic_update", "Zuka",$data);
				}else{
		    		$this->model_vote_management->updateTopic($top_id,$cat_id,$top_name,$top_desc,$max_item);
		       		redirect(site_url("vote_management/voteTopic"));
				}
			}else{
				redirect(site_url("vote_management/updateTopicForm/".$top_id));
			}
	    }
	    
	    
		/*- SHOW ASSIGN ITEM FORM -*/
		function assignItemForm($id) {
	    	$this->load->model("model_vote_management");
	    	$allCategories=$this->model_vote_management->selectCategory();
	    	$data['categories']=$allCategories;
	    	$topics=$this->model_vote_management->selectTopic($id);
	    	$data['topics']=$topics;
	    	foreach($topics->result_array() as $topic){
	    		$cat_id=$topic["vote_item_cat_id"];
	    	}
			$categories=$allCategories->result();
	    	//$cat_id=(isset($_POST['selcat']))?$_POST['selcat']:$categories[0]->vote_item_cat_id;
	    	$data['items']=$this->model_vote_management->selectItembyCategory($cat_id);
	    	$data['curitems']=$this->model_vote_management->selectTopicItem($id);
	    	$data['cat_id']=$cat_id;
	       	$this->load->template("templates/general","zuka_votes/topic_item", "Zuka",$data);
	    }
	    
	    /* ASSIGN ITEM */
	    function assignItem(){
	    	$this->load->model("model_vote_management");
	    	$top_id=$_POST['txttopid'];
	    	$myItem=$_POST['myItem'];
	    	$itemCount=count($myItem);
	    	$this->model_vote_management->deleteTopicItem($top_id);
	    	for($i=0;$i<=$itemCount-1;$i++){
	    		$this->model_vote_management->insertTopicItem($top_id,$myItem[$i]);
	    	}
	    	redirect(site_url("vote_management/voteTopic"));
	    }
	    
	    
		   	/* --- VOTE EVENT --- */
	    
	    /* SELECT */
	    function voteEvent() {
	    	$this->load->model("model_vote_management");
	    	$data['events']=$this->model_vote_management->selectEvent();
	       	$this->load->template("templates/general","zuka_votes/vote_event", "Zuka",$data);
	    }
		    /*- SHOW ADD NEW FORM -*/
		function addNewEventForm() {
			$this->load->model("model_vote_management");
			$data['topics']=$this->model_vote_management->selectTopic();
	       	$this->load->template("templates/general","zuka_votes/vote_event_add", "Zuka",$data);
	    }
	    	/*- INSERT -*/
		function addEvent() {
			$this->load->model("model_vote_management");
			$top_id=$_POST['txttopic'];
			$start_date=$_POST['startdate'];
			$end_date=$_POST['enddate'];
			if($top_id!=""){
				$result=$this->model_vote_management->existingEvent($top_id,$start_date,$end_date);
				$data['error']=($result>=1)?"This event already exists":"";
				if($data['error']!=""){
					$data['top_id']=$top_id;
					$data['start_date']=$start_date;
					$data['end_date']=$end_date;
					$data['topics']=$this->model_vote_management->selectTopic();
					$this->load->template("templates/general","zuka_votes/vote_event_add", "Zuka",$data);
				}else{
					$this->model_vote_management->insertEvent($top_id,$start_date,$end_date);
		       		redirect(site_url("vote_management/voteEvent"));
				}
			}else{
				redirect(site_url("vote_management/addNewEventForm"));
			}

	    }
		    /*- DELETE -*/
		function deleteEvent($event_id) {
	    	$this->load->model("model_vote_management");
	    	$this->model_vote_management->deleteEvent($event_id);
	       	redirect('vote_management/voteEvent');
	    }
	    
			/*- SHOW UPDATE FORM -*/
		function updateEventForm($id) {
	    	$this->load->model("model_vote_management");
	    	$data['topics']=$this->model_vote_management->selectTopic();
	    	$selevent=$this->model_vote_management->selectEvent($id);
	    	$data['events']=$selevent;
	    	$myevents=$selevent->result();
	    	$data['start_date']=$myevents[0]->start_date;
			$data['end_date']=$myevents[0]->end_date;
	    	$data['top_id']=$myevents[0]->top_id;
	       	$this->load->template("templates/general","zuka_votes/vote_event_update", "Zuka",$data);
	    }
	    	/*- UPDATE -*/
		function updateEvent() {
			$this->load->model("model_vote_management");
			$event_id=$_POST['txtevent'];
			$top_id=$_POST['txttopic'];
			$start_date=$_POST['startdate'];
			$end_date=$_POST['enddate'];
			if($top_id!=""){
				$result=$this->model_vote_management->existingEvent($top_id,$start_date,$end_date,$event_id);
				$data['error']=($result>=1)?"This event already exists":"";
				if($data['error']!=""){
					$data['event_id']=$event_id;
					$data['top_id']=$top_id;
					$data['start_date']=$start_date;
					$data['end_date']=$end_date;
	    			$data['events']=$this->model_vote_management->selectEvent($event_id);
					$data['topics']=$this->model_vote_management->selectTopic();
					$this->load->template("templates/general","zuka_votes/vote_event_update", "Zuka",$data);
				}else{
					$this->model_vote_management->updateEvent($event_id,$top_id,$start_date,$end_date);
		       		redirect(site_url("vote_management/voteEvent"));
				}
			}else{
				redirect(site_url("vote_management/voteEvent"));
			}

	    }
	}
?>